
var ns, toast,Dialog;
var app = new Vue({
    el: '#app',
    data: {
        OrderID: '',
        paytype: 0,// 默认为空0 //支付完成，验证付款情况
        orderlist: [],
        ordersummary: {
            totalprice: 0,
            totalpricedesc: ''
        },
        form:{},
        // form:{
        //   id:'',
        // },
        time:30*60*1000,
        show:false,
        showsend:false,//发货弹窗
        SYTime:'',

    },
    methods: {
        get_data: function() {
            var that = this;
            var option={
                  action: 'getorderdetail',
                  OrderID: that.OrderID,
            };
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        that.form = data.data;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        open_product: function(item) {
            var that = this;
            if (item.ProductTypeID == 10) {
                return;
            }
            if (item.ProductTypeID == 5) {
                ns.openWin('servicedetail_frm', item.title, {
                    title: item.title,
                    url: 'servicedetail_frm.html',
                    id: item.productid
                }, { needlogin: true });
                return;
            }
            var type = item.ProductOrderType || 16;
            ns.openWin('dl_product_frm', item.title, {
                id: item.productid,
                type: type
            });
        },
        open_business: function(id) {
            var that = this;
            ns.openWin('business_frm', '商家详情', {
                title: '商家详情',
                url: 'business_frm.html',
                id: id,
                searchtype: 2
            });
        },
        //打开物流信息
        do_openStep:function(){
            var that = this;
            ns.openWin('logistics_frm', '物流信息',{
                id:that.OrderID,
                AddressDetailInfo:that.form.AddressDetailInfo,

            });
        },
        //发货
        do_sendProduct:function(){
            var that=this;
            that.showsend=true;
        },
        beforeClose_send:function(action, done){
            var that=this;
            if(action==='confirm'){
                var option={};
                option.OrderID=that.OrderID;
                option.action='ordersend';
                ns.post(option, function(succeed, data, err) {
                    if(succeed){
                        if(data.status){
                            api.toast({
                                msg: '发货成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.get_data();
                            api.sendEvent({
                                name:'shopmsgevent'
                            });
                        }else{
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
                that.showsend=false;
                done();
            }else if(action==='cancel'){
                done();
            }
        },

        //取消订单
        do_removeorder:function(){
            var that=this;
            that.show=true;
        },
        //取消订单
        beforeClose:function(action,done){
            var that=this;
            if(action==='confirm'){
                var option={};
                option.OrderID=that.form.ID;
                option.action='cancelorder';
                ns.post(option, function(succeed, data, err) {
                    if(succeed){
                        if(data.status){
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.get_data();
                            api.sendEvent({
                                name:'shopmsgevent'
                            });
                        }
                    }else{
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
                that.show=false;
                done();
            }else if(action==='cancel'){
                done();
            }
        },
        //倒计时
        DealTime: function () {
            var that = this;
            that.form.AddTime = that.form.AddTime.replace(/-/g, "/");
            var oldTime = new Date(that.form.AddTime);
            oldTime.setMinutes(oldTime.getMinutes() + 30);
            that.AddTime = oldTime;
            that.setEndTime();
        },
        setEndTime: function () {
            var that = this;
            var interval = setInterval(function timestampToTime() {
                var date = (new Date(that.AddTime)) - (new Date()); //计算剩余的毫秒数
                if (date <= 0) {
                    that.isEnd = true;
                    clearInterval(interval);
                    ns.alert({
                        msg: '支付时间已超时，请重新提交订单',
                        buttons: ['知道了']
                    }, function () {
                        that.onCancelOrder();//调用取消订单接口
                        ns.closeWin();
                    })
                } else {
                    var timeh = parseInt(date / 1000 / 60 / 60 % 24, 10);
                    if (timeh < 10) {
                        timeh = "0" + timeh
                    }
                    var timem = parseInt(date / 1000 / 60 % 60, 10);//计算剩余的分钟
                    if (timem < 10) {
                        timem = "0" + timem
                    }
                    var times = parseInt(date / 1000 % 60, 10);//计算剩余的秒数
                    if (times < 10) {
                        times = "0" + times
                    }

                    if (timeh == '00') {
                        that.SYTime = timem + '分' + times + '秒';
                    }
                    if (timeh != '00') {
                        that.SYTime = timeh + '小时' + timem + '分' + times + '秒';
                    }
                }
            }, 1000);
        },

        // 修改费用
        do_editorder:function(){
            var that = this;
            ns.openWin('order_detail_edit', '修改费用',{
                OrderID:that.form.ID,
                ShipRateAmount:that.form.ShipRateAmount,
                ShipTimeAmount:that.form.ShipTimeAmount,
                PackingFee:that.form.PackingFee,
                TotalCost:that.form.TotalCost,
            });
        },

        //复制订单编号
        copy_txt:function(){
            var test=document.getElementById("dindan");
            test.select(); 
            document.execCommand("copy"); // 执行复制命令
            api.toast({
                msg: '复制成功',
                duration: 2000,
                location: 'bottom'
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.OrderID = api.pageParam.id;
    app.get_data();
    api.addEventListener({
        name: 'edit_fee'
    }, function(ret, err) {
        app.get_data();
    });
};
